import 'package:deer/res/colors.dart';
import 'package:deer/res/styles.dart';
import 'package:deer/routers/fluro_navigator.dart';
import 'package:deer/util/utils.dart';
import 'package:deer/widgets/app_bar.dart';
import 'package:deer/widgets/text_field.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter/foundation.dart';
import 'package:keyboard_actions/keyboard_actions.dart';
import 'package:oktoast/oktoast.dart';
import 'login_router.dart';

class SMSLogin extends StatefulWidget {
  @override
  _SMSLoginState createState() => _SMSLoginState();
}

class _SMSLoginState extends State<SMSLogin> {
  TextEditingController _nameController = TextEditingController();
  TextEditingController _codeController = TextEditingController();
  final FocusNode _nodeText1 = FocusNode();
  final FocusNode _nodeText2 = FocusNode();
  FocusScopeNode focusScopeNode;
  bool _isClick = false;
  bool _isStartGetCode = false;

  @override
  void initState() {
    super.initState();
    _nameController.addListener(_verify);
    _codeController.addListener(_verify);
  }

  void _login() {}

  void _verify() {
    String name = _nameController.text;
    String code = _codeController.text;
    bool isClick = true;
    if (name.isEmpty || name.length < 11) {
      isClick = false;
    }
    if (code.isEmpty) {
      isClick = false;
    }

    if (isClick != _isClick) {
      setState(() {
        _isClick = isClick;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(),
      body: defaultTargetPlatform == TargetPlatform.iOS
          ? FormKeyboardActions(
              child: _buildBody(),
            )
          : SingleChildScrollView(
              child: _buildBody(),
            ),
    );
  }

  _buildBody() {
    return GestureDetector(
      onTap: () {
        if (focusScopeNode == null) {
          focusScopeNode = FocusScope.of(context);
        }
        _nodeText1.unfocus();
        _nodeText2.unfocus();
      },
      child: Container(
        color: Colors.white, //必须设置颜色，否则点击空白处收起键盘不起作用
        child: Padding(
          padding: EdgeInsets.only(top: 20, left: 16, right: 16),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Text('验证码登录', style: TextStyles.textBoldDark26),
              Gaps.vGap(16),
              MyTextField(
                keyboardType: TextInputType.phone,
                controller: _nameController,
                hintText: '请输入账号',
                maxLength: 11,
                focusNode: _nodeText1,
              ),
              Gaps.vGap(10),
              MyTextField(
                keyboardType: TextInputType.number,
                controller: _codeController,
                hintText: '请输入验证码',
                maxLength: 4,
                focusNode: _nodeText2,
                config:
                    Utils.getKeyboardActionsConfig([_nodeText1, _nodeText2]),
                isStartGetCode: _isStartGetCode,
                getVCode: () {
                  if (_nameController.text.isEmpty) {
                    showToast('请输入电话号码');
                  } else if (_nameController.text.length < 11) {
                    showToast('请输入正确的电话号码');
                  } else {
                    setState(() {
                      _isStartGetCode = true;
                    });
                  }
                },
              ),
              Gaps.vGap(10),
              Container(
                alignment: Alignment.centerLeft,
                child: RichText(
                  text: TextSpan(
                      text: '提示：未注册账号的手机号，请先',
                      style: TextStyles.textGray14,
                      children: <TextSpan>[
                        new TextSpan(
                          text: '注册',
                          style: TextStyle(color: Colours.text_red),
                          recognizer: TapGestureRecognizer()..onTap = () {
                            NavigatorUtils.push(context, LoginRouter.registerPage);
                          },
                        )
                      ]),
                ),
              ),
              Gaps.vGap(24),
              Container(
                width: double.infinity,
                height: 44,
                child: FlatButton(
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(3)),
                  color: Colours.app_main,
                  textColor: Colors.white,
                  disabledTextColor: Colours.login_text_disabled,
                  disabledColor: Colours.login_button_disabled,
                  child: Text('登录', style: TextStyle(fontSize: 18)),
                  onPressed: _isClick ? _login : null,
                ),
              ),
              Container(
                alignment: Alignment.centerRight,
                height: 40,
                child: GestureDetector(
                  child: Text(
                    '忘记密码',
                    style: TextStyles.textGray12,
                  ),
                  onTap: () {
                    NavigatorUtils.push(context, LoginRouter.resetPasswordPage);
                  },
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}
